<?php
namespace apiadmin\modules\v1\controllers\trade;
use apiadmin\modules\v1\controllers\CoreController;
use common\models\GroupDetail;
use common\models\TradeClub;
use common\models\TradeCommodity;

class TradeClubController extends CoreController{

    public function actionListInfo()
    {

        $where  = $this->formartWhere();

        $params = array(
            'field'	=> ['*'],
            'order' => 'create_time desc',
            'page'	=> $this->request('page','1'),
            'limit' => $this->request('page_size',10),
        );

        $list =TradeClub::dataList($where,$params);
        $pages = TradeClub::$pages;
        $this->out('列表',$list,array('pages'=>$pages));
    }

    //组装条件
    public function formartWhere()
    {
        $where = [];
        $whereAnd = [];
        $searchKeys = json_decode($this->request('search'),1);

        if(!$searchKeys) return array('where'=>$where,'whereAnd'=>$whereAnd);
        foreach($searchKeys as $k=>$val)
        {
            if(!$val) continue;
            if($k=='date')
            {
                if(!$val['0'] || !$val['1']) continue;
                $whereAnd[] = ['between', 'create_time', date('Y-m-d',strtotime($val[0])),date('Y-m-d',strtotime($val[1]))];
            }elseif ($k=='member_name') {
                $whereAnd[] = ['like',$k,$val];
            }else
            {
                $where[$k] = $val;
            }
        }

        return array('where'=>$where,'and'=>$whereAnd);
    }

    public function actionInfo()
    {
        if(!$id = $this->request('commodity_id')) $this->error('参数错误');
        $data = TradeClub::getOne(['commodity_id'=>$id]);
        $this->out('俱乐部:'.$id,$data);
    }

    public function actionSave(){
        $model = new TradeClub;
        if($id = $this->request('commodity_id'))
            $model = TradeClub::findOne($id);
        else
            $model->create_time = date('Y-m-d H:i:s',time());

        $data = $this->request;
        $model->update_time = date('Y-m-d H:i:s',time());

        if($model->load($data,'') && $model->save())
        {
            $this->out('操作成功');
        }
        $this->error('操作失败');
    }

}